//
//  moneybuyCV.m
//  YaoHeAPP
//
//  Created by xyh on 16/3/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "moneybuyCV.h"

#import "Masonry.h"

#import "TPKeyboardAvoidingTableView.h"

#import "Controller.h"

#import "Tool.h"

#import "GeneraldetailsVC.h"


#import "chongzhiVC.h"
#import "xianhuoDatailView.h"

#import "NSString+formatWithFloat.h"

#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height
#define bgVCcolor [UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:244.f/255.f alpha:1]

@interface moneybuyCV ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    NSArray *array;
    BOOL iscanmoney;//余额是否充足
    
    BOOL open;//关闭
    
    NSString *mima;
    
    NSString *qiankuan;
}
@property(nonatomic,strong) TPKeyboardAvoidingTableView *tableview;  //定义大表格
@end

@implementation moneybuyCV

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"moneybuyCV"];
   [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"moneybuyCV"];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(140);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"余额付款";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.top.offset(31);
    }];
    
    
    //下部按钮
    UIView *bootmView=[[UIView alloc]init];
    bootmView.backgroundColor=[UIColor clearColor];
    bootmView.frame=CGRectMake(0,screenHeight-50, screenWidth, 50);
    [self.view addSubview:bootmView];
    

    
    UILabel *leftlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth-20, 30)];
    leftlabel.font=[UIFont systemFontOfSize:14];
    leftlabel.textAlignment=NSTextAlignmentCenter;
    leftlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
    [bootmView addSubview:leftlabel];
    NSString *ss=[NSString stringWithFormat:@"如有问题请拨打客服热线:  400-885-0054"];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:ss];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@":"].location+1,noteStr.length-12);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1] range:redRange];
    [leftlabel setAttributedText:noteStr];

    UIButton *iphonebut=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2, 0, screenWidth/2, 30)];
    iphonebut.backgroundColor=[UIColor clearColor];
    [iphonebut addTarget:self action:@selector(iphoneBUtAction) forControlEvents:UIControlEventTouchUpInside];
    [bootmView addSubview:iphonebut];
    
    
    //顶部数组
    
    //中间线长度
    NSInteger online=(screenWidth-(30*3)-(40*2))/2;
    NSInteger jianju=(screenWidth-(60*3)-(25*2))/2;
    NSInteger yuanWandH=30;
    NSArray *topArray=@[@"确认订单",@"支付订单",@"已结算"];
    for (int i=0; i<topArray.count; i++) {
        UILabel *yuan=[[UILabel alloc]init];
        yuan.frame=CGRectMake(40+(30+online)*i, 140-15-30-10-20, yuanWandH, yuanWandH);
        yuan.layer.masksToBounds=YES;
        yuan.text=[NSString stringWithFormat:@"%d",i+1];
        yuan.textAlignment=NSTextAlignmentCenter;
        yuan.textColor=[UIColor whiteColor];
        yuan.tag=10+i;
        yuan.layer.cornerRadius=15;
        
        [navcBar addSubview:yuan];
        
        
        UILabel *toplable=[[UILabel alloc]init];
        toplable.frame=CGRectMake(25+(60+jianju)*i, 140-15-30-10-20+30+5, 60, 30);
        toplable.text=topArray[i];
        toplable.textAlignment=NSTextAlignmentCenter;
        toplable.font=[UIFont systemFontOfSize:14];
        toplable.textColor=[UIColor whiteColor];
        toplable.tag=100+i;
        
        if (i==0||i==1) {
            
            yuan.backgroundColor=[UIColor colorWithRed:146.f/255.f green:198.f/255.f blue:35.f/255.f alpha:1];
            yuan.alpha=1;
            
            toplable.alpha=1;
        }else{
            yuan.backgroundColor=[UIColor colorWithRed:91.f/255.f green:140.f/255.f blue:152.f/255.f alpha:0.5];
            yuan.alpha=0.5;
            
            toplable.alpha=0.5;
        }
        
        [navcBar addSubview:toplable];
        
        
    }
    
    for (int j=0; j<topArray.count-1; j++) {
        
        UIImageView *onlineimg=[[UIImageView alloc]init];
        
        if (j==0) {
            onlineimg.backgroundColor=[UIColor colorWithRed:146.f/255.f green:198.f/255.f blue:35.f/255.f alpha:1];
        }else{
            onlineimg.backgroundColor=[UIColor colorWithRed:91.f/255.f green:140.f/255.f blue:152.f/255.f alpha:0.5];
        }
        
        onlineimg.frame=CGRectMake(40+yuanWandH*(j+1)+online*j, 140-15-30-10-20+15, online, 1);
        [navcBar addSubview:onlineimg];
        
    }

    
    //获取资金余额
    [self getmoneyNUM];
//    self.kamoney=@"255000.00";
    
    //判断是否余额充足
//    iscanmoney=YES;
    open=NO;
    
//    array=@[@"订单金额",@"可用余额:",@"信用行支付密码:"];
}

//拨打电话
- (void)iphoneBUtAction {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"客服工作时间:周一到周五\n(8:30~17:30)" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"呼叫客服", nil];
    alertView.tag=10;
    [alertView show];
}

#pragma mark 呼叫客服
- (void)callCustomerService {
    UIWebView *webview = [[UIWebView alloc] init];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:4008850054"]]];
    [self.view addSubview:webview];
}



-(void)getmoneyNUM
{
   //获取 需要支付的金额 以及 账户余额
        NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
        [di setValue:self.pay_type forKey:@"pay_type"];
    
    if ([self.pay_type isEqualToString:@"1"]) {
       [di setValue:self.batch_id forKey:@"order_ids"];
    }else if([self.pay_type isEqualToString:@"10"]){
        [di setValue:self.intent_order_id forKey:@"order_ids"];
    }else if([self.pay_type isEqualToString:@"5"]){
        [di setValue:self.order_ids forKey:@"order_ids"];
    }
    
    //要判断什么类型的然后穿的参数不同
    [Controller requestWithURL:NEEDPAYMONEY jsondic:di finishBlock:^(ClassData* data) {
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]) {
            self.moneyStr=[NSString  stringWithFormat:@"%@",data.data[@"pay_money"]];
            self.kamoney=[NSString  stringWithFormat:@"%@",data.data[@"account_money"]];
            
            float kamoney=self.kamoney.floatValue;
            float money=self.moneyStr.floatValue;
            NSDecimalNumber *kamoneyDN = [[NSDecimalNumber alloc] initWithString:self.kamoney];//卡上钱数
            NSDecimalNumber *moneyDN = [[NSDecimalNumber alloc] initWithString:self.moneyStr];//需要支付钱数
            
            if (money>kamoney) {
                //余额不足
                //判断是否余额充足
                NSDecimalNumber *cha=[moneyDN decimalNumberBySubtracting:kamoneyDN];
                iscanmoney=NO;
                qiankuan=[NSString stringWithFormat:@"%@元",[NSString notRounding:cha afterPoint:2]];
                
                
            }else{
                //余额充足
                iscanmoney=YES;
            }
            
            //创建表格
            _tableview = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectMake(0,145, self.view.frame.size.width, self.view.frame.size.height-140-50) style:UITableViewStylePlain];
            _tableview.backgroundColor = RGBCOLOR(230, 232, 241);
            _tableview.delegate = self;
            _tableview.dataSource = self;
            _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
            _tableview.scrollEnabled = NO;
            [self.view addSubview:_tableview];
        } else {
            ALERTSHOW(data.message);
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 4;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==3) {
        return 80;
    }
    
    if (indexPath.row==2) {
        if (iscanmoney==YES) {
            return 80;
        }else{
        
            return 50;
        }
    }
    
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    NSString *identifier = [NSString stringWithFormat:@"TimeLineCell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    
    //自定义cell类有图片
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            //        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            
            if (indexPath.row==0||indexPath.row==1) {
                
                
                UILabel *leftlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 150, 20)];
                leftlabel.font=[UIFont systemFontOfSize:14];
                leftlabel.textAlignment=NSTextAlignmentLeft;
                leftlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:leftlabel];
                
                UILabel *rightlabel=[[UILabel alloc]initWithFrame:CGRectMake(150, 15, screenWidth-150-10, 20)];
                rightlabel.textAlignment=NSTextAlignmentRight;
                rightlabel.font=[UIFont systemFontOfSize:12];
                rightlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:rightlabel];
                rightlabel.tag=500+indexPath.row;
                //分割线
                UIView *vwimg=[[UIView alloc]init];
                vwimg.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                vwimg.frame=CGRectMake(10, 50-1, screenWidth-1, 1);
                [cell addSubview:vwimg];
                
                if (indexPath.row==0) {
                    leftlabel.text=@"订单金额";
                     NSDecimalNumber *moneyStrDN = [[NSDecimalNumber alloc] initWithString:self.moneyStr];//订单金额
                    rightlabel.text=[NSString stringWithFormat:@"￥%@元",[NSString notRounding:moneyStrDN afterPoint:2]];
                    rightlabel.textColor=[UIColor redColor];
                }
                if (indexPath.row==1) {
                    if (iscanmoney==YES) {
                        
                        leftlabel.text=@"可用余额";
                        
                    }else{

                        NSString *ss=[NSString stringWithFormat:@"可用余额:  余额不足"];
                        NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:ss];
                        NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@":"].location+1,noteStr.length-5 );
                        [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
                        [leftlabel setAttributedText:noteStr];
                    }
                    NSDecimalNumber *kamoneyDN = [[NSDecimalNumber alloc] initWithString:self.kamoney];//卡上钱数
                    rightlabel.text=[NSString stringWithFormat:@"￥%@元",[NSString notRounding:kamoneyDN afterPoint:2]];
                    
                }

                
            }
            
            if (indexPath.row==2) {
                
                if (iscanmoney==YES) {
                    
                    UIView *bacView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
                    bacView.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                    [cell addSubview:bacView];
                    
                    UILabel *la=[[UILabel alloc]init];
                    la.frame=CGRectMake(10, 10, screenWidth-20, 20);
                    la.font=[UIFont systemFontOfSize:10];
                    la.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    la.text=@"您使用了虚拟资金，为了保障您的资金安全，请进行安全验证。";
                    [bacView addSubview:la];

                    UILabel *leftlabel2=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 120, 20)];
                    leftlabel2.text=@"信用行支付密码:";
                    leftlabel2.font=[UIFont systemFontOfSize:14];
                    leftlabel2.textAlignment=NSTextAlignmentLeft;
                    leftlabel2.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    [cell addSubview:leftlabel2];
                
                    
                    
                    UITextField *password=[[UITextField alloc]init];
                    password.frame=CGRectMake(120, 40, screenWidth-120-50, 30);
                    
                    password.tag=400+indexPath.row;
                    
                    password.borderStyle=UITextBorderStyleNone;
                    password.placeholder=@"请输入密码";
                    password.clearButtonMode=UITextFieldViewModeWhileEditing;
                    password.secureTextEntry=YES;
                    password.font=[UIFont systemFontOfSize:14];
                    password.delegate=self;
                    [password addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
                    [password addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventEditingDidEndOnExit];
                    [cell addSubview:password];
                    
                    
                    
                    UIButton *seeBUt=[UIButton buttonWithType:UIButtonTypeCustom];
                    seeBUt.selected=NO;
                    seeBUt.tag=300+indexPath.row;
                    
                    [seeBUt setBackgroundImage:[UIImage imageNamed:@"ic_close_normal"] forState:UIControlStateNormal];
                    [seeBUt addTarget:self action:@selector(seeBUtAction:) forControlEvents:UIControlEventTouchUpInside];
                    seeBUt.frame=CGRectMake(screenWidth-50, 45, 30, 30);
                    seeBUt.layer.masksToBounds=YES;
                    seeBUt.layer.cornerRadius=12.5;
                    [cell addSubview:seeBUt];

                
                    
                    
                    
                    //分割线
                    UIView *vwimg2=[[UIView alloc]init];
                    vwimg2.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                    vwimg2.frame=CGRectMake(10, 80-1, screenWidth-1, 1);
                    [cell addSubview:vwimg2];

                    
                }else{
                
                    UILabel *leftlabel2=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 150, 20)];
                    leftlabel2.text=@"还需打款:";
                    leftlabel2.font=[UIFont systemFontOfSize:14];
                    leftlabel2.textAlignment=NSTextAlignmentLeft;
                    leftlabel2.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    [cell addSubview:leftlabel2];
                    
                    UILabel *rightlabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 15, screenWidth-150-10, 20)];
                    rightlabel2.textAlignment=NSTextAlignmentRight;
                    rightlabel2.font=[UIFont systemFontOfSize:12];
//                    rightlabel2.text=@"￥1452300.00元";
                    rightlabel2.tag=900+indexPath.row;
                    rightlabel2.textColor=[UIColor redColor];
                    [cell addSubview:rightlabel2];
                    
                    //分割线
                    UIView *vwimg2=[[UIView alloc]init];
                    vwimg2.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                    vwimg2.frame=CGRectMake(10, 50-1, screenWidth-1, 1);
                    [cell addSubview:vwimg2];

                    
                
                }
            }
            
                if (indexPath.row==3) {
                    cell.backgroundColor=[UIColor clearColor];
                    
                    UIButton *but=[[UIButton alloc]initWithFrame:CGRectMake((screenWidth-200)/2, 40, 200, 40)];
                    but.backgroundColor=[UIColor colorWithRed:251.f/255.f green:87.f/255.f blue:60.f/255.f alpha:1];
                    but.titleLabel.font=[UIFont systemFontOfSize:14];
                    [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    but.layer.masksToBounds=YES;
                    but.layer.cornerRadius=20;
                    [but addTarget:self action:@selector(BUtAction) forControlEvents:UIControlEventTouchUpInside];
                    [cell addSubview:but];
 
                    if (iscanmoney==YES) {
                        
                        [but setTitle:@"立即支付" forState:UIControlStateNormal];
                    }else{
                        [but setTitle:@"去充值" forState:UIControlStateNormal];
                    }
                    
                    
                    
                }
                
                
            }
    
    if (indexPath.row==0) {
        
        UILabel *money=[cell viewWithTag:500+indexPath.row];
        NSDecimalNumber *moneyStrDN = [[NSDecimalNumber alloc] initWithString:self.moneyStr];//订单金额
        money.text=[NSString stringWithFormat:@"￥%@元",[NSString notRounding:moneyStrDN afterPoint:2]];
        
    }
    if (indexPath.row==1) {
        
        UILabel *money=[cell viewWithTag:500+indexPath.row];
        NSDecimalNumber *kamoneyDN = [[NSDecimalNumber alloc] initWithString:self.kamoney];//卡上钱数
        money.text=[NSString stringWithFormat:@"￥%@元",[NSString notRounding:kamoneyDN afterPoint:2]];
//        money.text=[NSString stringWithFormat:@"￥%@元",self.kamoney];
        
    }
    
    if (indexPath.row==2) {
        
        if (iscanmoney==NO) {
            UILabel *money=[cell viewWithTag:900+indexPath.row];
            money.text=qiankuan;
        
        }else{
            
            UITextField *passw=[cell viewWithTag:400+indexPath.row];
            UIButton *butop=[cell viewWithTag:300+indexPath.row];
            
            if (open==YES) {
                [butop setBackgroundImage:[UIImage imageNamed:@"ic_open_pressed"] forState:UIControlStateNormal];
                passw.secureTextEntry=NO;
                passw.text=mima;
            }else{
                [butop setBackgroundImage:[UIImage imageNamed:@"ic_close_normal"] forState:UIControlStateNormal];
                passw.secureTextEntry=YES;
                passw.text=mima;
                
            }
        
        }
    }
    
        return cell;

}

-(void)seeBUtAction:(UIButton *)seebut
{
    
    UITableViewCell * cell = (UITableViewCell *)[seebut superview];
    NSIndexPath * path = [self.tableview indexPathForCell:cell];
//    UIButton *butop=[cell viewWithTag:300+path.row];
    
    if (open==NO) {
//        butop.selected=YES;
        open=YES;
    }else{
//        butop.selected=NO;
        open=NO;
    }
    
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:path,nil] withRowAnimation:UITableViewRowAnimationNone];

}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    mima=textField.text;
    
//    [UIView animateWithDuration:0.25 animations:^{
//        self.view.frame=CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height);
//    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    mima=textField.text;
    
//    [UIView animateWithDuration:0.25 animations:^{
//        self.view.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{  //string就是此时输入的那个字符 textField就是此时正在输入的那个输入框 返回YES就是可以改变输入框的值 NO相反
    
    //获取控件所在位置
    mima=string;
    mima=textField.text;
    return YES;
    
}

-(void)textFieldDidChanged:(UITextField *)textField
{
 mima=textField.text;


}
//键盘弹下
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)disMiss
{
    return;
}



//    支付
-(void)BUtAction
{
    if (iscanmoney==YES) {
        
        
        [MobClick event:@"balanceOfPayment"];
        
//        立即支付
//        支付现在三种  一种是 确认完订单直接支付   第二种是待支付订单支付  第三种是待还款订单支付 第一种用  batch_id  第二种用  intent_order_id   第三种用  order_id   pay_type  1 就是第一种状态  10 是第二种   5 是第三种
        
    
        NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
        
        if ([self.pay_type isEqualToString:@"1"]) {
            
            [di setValue:self.batch_id forKey:@"order_id"];
            
        }else if([self.pay_type isEqualToString:@"10"]){
            
            [di setValue:self.intent_order_id forKey:@"order_id"];
            
        }else if ([self.pay_type isEqualToString:@"5"]){
            [di setValue:self.order_ids forKey:@"order_id"];
            
            
        }

        [di setValue:self.pay_type forKey:@"pay_type"];
        [di setValue:@"" forKey:@"member_id"];
        [di setValue:@"" forKey:@"mobile"];
        
        NSString *zhuanhuanmima=[self hexStringFromString:mima];
        
        [di setValue:zhuanhuanmima forKey:@"pay_password"];//支付密码
        
         [Controller requestWithURL:PAYORDER jsondic:di finishBlock:^(ClassData* data) {
            
            if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
            {
                if (self.ordernum>1) {
                    //初始化AlertView
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付成功"
                                                                    message:@"你的订单已支付成功，你可以在个人中心“现货订单”中查看订单的进展"
                                                                   delegate:self
                                                          cancelButtonTitle:@"继续购物"
                                                          otherButtonTitles:@"关闭",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 4;
                    //只读属性，看AlertView是否可见
                    [alert show];

                    
                }else{
                
                //初始化AlertView
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付成功"
                                                                message:@"你的订单已支付成功，你可以在个人中心“现货订单”中查看订单的进展"
                                                               delegate:self
                                                      cancelButtonTitle:@"继续购物"
                                                      otherButtonTitles:@"查看详情",nil];
                
                //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                alert.tag = 5;
                //只读属性，看AlertView是否可见
                [alert show];
                    
                }
                
//                //通知代理去响应页面刷新数据
//                if ([self.delegate respondsToSelector:@selector(uporderlist)]) {
//                    [self.delegate uporderlist];
//                }
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"huankuansucceslist" object:nil];
               
            }else
            {
                
                ALERTSHOW(data.message);
                
                
            }
        }];
        
        
    }else{
//        去充值
        chongzhiVC*czVC=[[chongzhiVC alloc]init];
        czVC.timest=self.odertime;
        czVC.moneyout=qiankuan;
        czVC.ismingxi=@"余额不足";
        [self.navigationController pushViewController:czVC animated:YES];

        
    }

    

}

//根据被点击按钮的索引处理点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == 4){
        if (buttonIndex == 0) {
            //继续购物
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:3];
            
        } else if (buttonIndex == 1) {
            
        }
    } else if (alertView.tag == 5) {
        if (buttonIndex == 0) {
            //继续购物
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:3];

        } else if (buttonIndex == 1) {
            //查看订单
            GeneraldetailsVC *generalVC = [[GeneraldetailsVC alloc] init];
            generalVC.intent_order_id = self.intent_order_id;
            [self.navigationController pushViewController:generalVC animated:YES];
        }
        
    } else if (alertView.tag == 0) {
        if (buttonIndex == 1) {
            
        } else if (buttonIndex == 0) {
            
            [MobClick event:@"giveUpPayment"];
            
            //取消
            // 情况1:返回到现货详情界面
            // 情况2:返回到待支付界面
            UIViewController *tempViewController = [self.navigationController viewControllers][1];
            [self.navigationController popToViewController:tempViewController animated:YES];
        }
    } else if (alertView.tag == 10) {
        if (0 == buttonIndex) {
            // do nothing
        } else {
            [self callCustomerService];
        }
    }
}

//普通字符串转换为十六进制的。

-(NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; 
    } 
    return hexStr; 
}

- (void)backLogin {
    //初始化AlertView
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                    message:@"确定要放弃付款吗？"
                                                   delegate:self
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:@"取消",nil];
    
    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
    alert.tag = 0;
    [alert show];
}

@end
